import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;


public class Gui extends JFrame{

	private static final long serialVersionUID = 1L;

	Gui(int n, int goal, int start){
		super();
		this.setLayout(new GridLayout(2,1));		
		/* 	Initialisiere DE*/
		if (start <4 && start >0)
			this.start = start;
		if (goal != this.start && goal <4 && goal >0)
			this.goal = goal;
		help = start - goal;
		if (help < 0)
			help *= -1;
		
		//RunGUI();
		logic = new Hanoi(n);
		gui = new GameFrame(this);

		/* Baue Panel*/
		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(3,1));
		undo = new JButton("Undo");
		solve = new JButton("Solve");
		p.add(undo);
		p.add(solve);
		
		undo.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				logic.undo();
				gui.repaint();
				refresh();
			}
		});
		
		solve.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				logic.solve(getN(), logic.getStart(),logic.getHelp(),logic.getGoal(), getThis());
			}
		});
		
		
		/* LIST */
        JList moves = new JList();
        moves.setModel(data);
        

		
		/* THIS */
		this.add(p);
		this.add(moves);
		this.setSize(200,500);
		this.setVisible(true);
		
	}
	/*
	private void RunGUI(){
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                gui = new Gui(this);
            }
        });
	}
	*/
	public void refresh(){
		data.clear();
        for (Move i : logic.getMove()){
        	data.addElement(i);
        }
        if(logic.getPicked()){
        	solve.setEnabled(false);
        	undo.setEnabled(false);
        }
        else{
        	solve.setEnabled(true);
        	undo.setEnabled(true);
        }
	}
	
	
	/* Getter */
	
	public Hanoi getGameLogic() {
		return logic;
	}
	public GameFrame getGui() {
		return gui;
	}
	public int getN() {
		return n;
	}
	public int getStart() {
		return start;
	}
	public int getHelp() {
		return help;
	}
	public int getGoal() {
		return goal;
	}
	public DefaultListModel getData() {
		return data;
	}

	
	private Gui getThis(){
		return this;
	}
	private Hanoi logic;
	private GameFrame gui;
	private int n =3;
	private int start = 1;
	private int help = 2;
	private int goal = 3;
	private DefaultListModel data = new DefaultListModel();
	private JButton undo;
	private JButton solve;
	
	public static void main(String[] args){
		new Gui(3,3,1);
	}
}
